package bingo.component.amchart.tags;

import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import net.bingosoft.common.json.JSONObject;

/**
 * Jun 25, 2008 5:26:09 PM
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:图表展现的标签
 * ChangeLog:
 */
public class ChartTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	private String variables;
	private String swfFilePath;
	private String displayDiv;
	private String settingsFile;
	private String arguments;
	private String chartId;
	private String path;
	private int height = 400;;
	private int width = 520;
	private String bgColor = "#FFFFFF";
	private String chartType;
	private String service;
	private String preloaderColor = "#000000";
	private String sqlKey;
	private Integer graphCount;

	public Integer getGraphCount() {
		return graphCount;
	}
	public void setGraphCount(Integer graphCount) {
		this.graphCount = graphCount;
	}
	public String getSqlKey() {
		return sqlKey;
	}
	public void setSqlKey(String sqlKey) {
		this.sqlKey = sqlKey;
	}
	@Override
	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		try {
			StringBuilder sb = new StringBuilder();
			appendLine(sb,"<!-- amchart script start-->");
			appendLine(sb,"<script type=\"text/javascript\">");
			appendLine(sb,"// <![CDATA[");
			//define chart
			sb.append("var chart = new SWFObject(")
				.append("\"").append(this.swfFilePath).append("\",")
				.append("\"").append(this.chartId).append("\",")
				.append("\"").append(this.width).append("\",")
				.append("\"").append(this.height).append("\",")
				.append("\"").append(8).append("\",")
				.append("\"").append(this.bgColor).append("\");\n");
			//add vars
			sb.append("\nchart.addVariable(\"path\",\"").append(path).append("\");");
			sb.append("\nchart.addVariable(\"settings_file\",escape(\"").append(settingsFile).append("\"));");
			//设置获取数据的URL
			addDataFile(arguments, sb);
			//添加其他的一些变量
			addVariables(variables, sb);
			//设置预加载颜色
			sb.append("\nchart.addVariable(\"preloader_color\",\"").append(preloaderColor).append("\");");
			//输入的指定的div
			sb.append("\nchart.write(\"").append(displayDiv).append("\");");
			sb.append("\n// ]]>");
			sb.append("\n</script>");
			sb.append("\n<!-- amchart script end-->");
			writer.print(sb);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}
	/**
	 * 输入一行
	 * @param sb
	 * @param content
	 */
	protected void appendLine(StringBuilder sb,String content){
		sb.append("\n").append(content).append("\n");
	}

	/**
	 * 添加其它的参数
	 * @param variables
	 * @param sb
	 */
	protected void addVariables(String variables,StringBuilder sb){
		if(variables == null) return;
		JSONObject vars;
		try {
			vars = new JSONObject(variables);
			Map<String,Object> varsMap = vars.getParams();
			for(String varKey : varsMap.keySet()){
				sb.append("\nchart.addVariable(\"").append(varKey).append("\",\"").append(varsMap.get(varKey)).append("\");");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 传入后台的json字符串
	 * @param argsJSON
	 * @param sb
	 */
	protected void addDataFile(String argsJSON,StringBuilder sb){
		String ctxPath = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
		sb.append("\nchart.addVariable(\"data_file\",escape(\"").append(ctxPath).append("/AmChartDataLoaderServlet?")
		.append("&chartType=").append(chartType)
		.append("&sqlKey=").append(sqlKey);
		if(service != null) {
			sb.append("service=").append(service);
		}
		if(graphCount != null) {
			sb.append("&graphCount=").append(graphCount);
		}
		if(argsJSON != null){
			try {
				JSONObject args = new JSONObject(argsJSON);
				Map<String ,Object> argsMap = args.getParams();
				for(String key : argsMap.keySet()){
					sb.append("&").append(key).append("=").append(argsMap.get(key));
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		sb.append("\"));");
	}


	public String getVariables() {
		return variables;
	}

	public void setVariables(String variables) {
		this.variables = variables;
	}

	public String getSwfFilePath() {
		return swfFilePath;
	}

	public void setSwfFilePath(String swfFilePath) {
		this.swfFilePath = swfFilePath;
	}

	public String getDisplayDiv() {
		return displayDiv;
	}

	public void setDisplayDiv(String displayDiv) {
		this.displayDiv = displayDiv;
	}

	public String getSettingsFile() {
		return settingsFile;
	}

	public void setSettingsFile(String settingsFile) {
		this.settingsFile = settingsFile;
	}

	public String getArguments() {
		return arguments;
	}

	public void setArguments(String arguments) {
		this.arguments = arguments;
	}

	public String getChartId() {
		return chartId;
	}

	public void setChartId(String chartId) {
		this.chartId = chartId;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public String getBgColor() {
		return bgColor;
	}

	public void setBgColor(String bgColor) {
		this.bgColor = bgColor;
	}

	public String getChartType() {
		return chartType;
	}

	public void setChartType(String chartType) {
		this.chartType = chartType;
	}

	public String getService() {
		return service;
	}

	public void setService(String service) {
		this.service = service;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public String getPreloaderColor() {
		return preloaderColor;
	}

	public void setPreloaderColor(String preloaderColor) {
		this.preloaderColor = preloaderColor;
	}
	@Override
	public void release() {
		
		super.release();
		this.variables = null;
		this.swfFilePath = null;
		this.displayDiv = null;
		this.settingsFile = null;
		this.arguments = null;
		this.chartId = null;
		this.path = null;
		this.height = 200;
		this.width = 260;
		this.bgColor = "#FFFFFF";
		this.chartType = null;
		this.service = null;
		this.preloaderColor = "#000000";
		this.sqlKey = null;
	}
//	public static void main(String[] args) {
//		for(Field f : ChartTag.class.getDeclaredFields()){
//			System.out.println("<attribute>");
//			System.out.println("\t<name>"+f.getName()+"</name>");
//			System.out.println("\t<required>true</required>");
//			System.out.println("\t<rtexprvalue>true</rtexprvalue>");
//			System.out.println("</attribute>");
//		}
//	}
	

}
